package com.ebt.m.proposal_v2.bean;

import android.text.TextUtils;

import com.ebt.m.commons.utils.Logger;
import com.ebt.m.proposal_v2.ProposalConstant;
import com.ebt.m.proposal_v2.base.StringExtractBase;
import com.ebt.m.proposal_v2.dao.response.BusinessConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * 险种
 *
 * @author archer.qi
 *         Created on 2017/5/12.
 */
public class InsuranceEntity extends StringExtractBase {

    /**
     * 存储当前的这个insuranceEntity其拥有者（insuredEntity）是否符合购买这个保险的条件，不符合为false，cellview上画横线
     */
    public boolean isValidAccordingToInsuredEntityAndInsuranceEntity;

    public BusinessConfig businessConfig;

    public int shouldSaveClient;

    /** 险种从属的被保人ID*/
    public List<String> insuredIds;

    /** 品牌ID*/
    public int brandId;

    /** 产品ID*/
    public int productId;

    /** 产品名称*/
    public String productName;

    /** 是否主险 1->Main */
    public int isMain;

    /** 缩略图*/
    public String thumbnail;

    /** 描述*/
    public String description;

    /** 排序位置*/
    public int sort;

    /** 保额*/
    public InsuranceOption coverage;

    /** 保费*/
    public InsuranceOption premium;

    /** 保障期间*/
    public InsuranceOption coveragePeriod;

    /** 缴费期间*/
    public InsuranceOption paymentPeriod;

    /** 附加险属性，用于判定是否符合条件*/
    public boolean isEnabled;

    /** 附加险属性，不符合条件的理由*/
    public String waringMessage;

    /**是否绑定主险*/
    public boolean isBindingMain;

    /*********
     * Field 为豁免而生
     ********/

    /** 险种代码： 主险代码为： *， 附加险为具体代码，如JM06*/
    public String planCode;

    /**
     * 附加险-险种类型： 1 普通附加险 ProposalConstant.RISK_TYPE_COMMON 2 豁免附加险 ProposalConstant.RISK_TYPE_EXEMPT 3 普通附加险 指定被保人为投保人Applicant（但不指定产品）
     */
    public int riskType;

    /**豁免人： 投保人->Applicant  or 被保人->Insured  */
    public String exemptPerson;

    /**豁免险种代码*/
    public List<String> exemptProduct;

    /**
     * 执行查询或上传请求时，使用该数组； 逻辑使用时，使用coverage等Option对象；因此在查询后，或上传前，需要解析与装箱
     * 执行PUT时，如生成计划书，调用reparseFeatureData()将option对象装箱到featureData中
     * 执行GET时，如查询计划书，调用parseFeatureData()将featureData解析到coverage等option对象中
     */
    public List<InsuranceOption> featureData;

    /**
     * 除了四个基本选项之外的其他参数，在计算器返回之后返回的10几项保险参数，除了Premium, Coverage, CoveragePeriod, PaymentPeriod之外，都保存与当前数组中
     * 最后在ProposalParam中格式化并上传
     */
    public List<InsuranceOption> extraData;

    public String accMaxAge;
    public String accMinAge;
    public String accOccupation;
    public String accSex;

    public InsuranceEntity(){

    }

    public InsuranceEntity(InsuranceEntity org){
        this.isValidAccordingToInsuredEntityAndInsuranceEntity = org.isValidAccordingToInsuredEntityAndInsuranceEntity;
        this.accMaxAge = org.accMaxAge;
        this.accMinAge = org.accMinAge;
        this.accOccupation = org.accOccupation;
        this.accSex = org.accSex;

        this.businessConfig = new BusinessConfig(org.businessConfig);
        this.shouldSaveClient = org.shouldSaveClient;
        this.brandId = org.brandId;
        this.productId = org.productId;
        this.productName = org.productName;
        this.isMain =org.isMain;
        this.thumbnail = org.thumbnail;
        this.description = org.description;
        this.sort = org.sort;
        this.coverage = new InsuranceOption(coverage);

        this.premium = new InsuranceOption(premium);
        this.coveragePeriod = new InsuranceOption(coveragePeriod);
        this.paymentPeriod = new InsuranceOption(paymentPeriod);

        this.isEnabled = org.isEnabled;
        this.waringMessage = org.waringMessage;
        this.isBindingMain = org.isBindingMain;
        this.planCode = org.planCode;
        this.riskType = org.riskType;
        this.exemptPerson = org.exemptPerson;
        this.insuredIds = new ArrayList<>();
        if (org.insuredIds != null){
            for (String string : org.insuredIds){
                this.insuredIds.add(string);
            }
        }

        this.exemptProduct = new ArrayList<>();
        if (org.exemptProduct != null){
            for (String string : org.exemptProduct){
                this.exemptProduct.add(string);
            }
        }

        this.featureData = new ArrayList<>();
        if (org.featureData != null){
            for (InsuranceOption insuranceOption : org.featureData){
                this.featureData.add(new InsuranceOption(insuranceOption));
            }
        }

        this.extraData = new ArrayList<>();
        if (org.extraData != null){
            for (InsuranceOption insuranceOption : org.extraData){
                this.extraData.add(new InsuranceOption(insuranceOption));
            }
        }

    }

    public String getAccMaxAge() {
        return accMaxAge;
    }

    public void setAccMaxAge(String accMaxAge) {
        this.accMaxAge = accMaxAge;
    }

    public String getAccMinAge() {
        return accMinAge;
    }

    public void setAccMinAge(String accMinAge) {
        this.accMinAge = accMinAge;
    }

    public String getAccOccupation() {
        return accOccupation;
    }

    public void setAccOccupation(String accOccupation) {
        this.accOccupation = accOccupation;
    }

    public String getAccSex() {
        return accSex;
    }

    public void setAccSex(String accSex) {
        this.accSex = accSex;
    }

    public BusinessConfig getBusinessConfig() {
        return businessConfig;
    }

    public void setBusinessConfig(BusinessConfig businessConfig) {
        this.businessConfig = businessConfig;
    }

    public boolean isValidAccordingToInsuredEntityAndInsuranceEntity() {
        return isValidAccordingToInsuredEntityAndInsuranceEntity;
    }

    public void setValidAccordingToInsuredEntityAndInsuranceEntity(boolean valid) {
        isValidAccordingToInsuredEntityAndInsuranceEntity = valid;
    }

    public void addExtraData(List<InsuranceOption> extras) {
        if (extraData != null) {
            extraData.clear();
        }
        extraData = extras;
    }

    public void resetExtras() {
        if (extraData == null) {
            extraData = new ArrayList<>();
        } else {
            extraData.clear();
        }
    }

    public void addExtraItem(InsuranceOption option) {
        if (option != null && extraData != null) {
            extraData.add(option);
        }
    }

    @Override
    public String getDisplayName() {
        return productName;
    }

    public boolean isMain() {
        return isMain == 1;
    }

    /**
     * 解析featureData到独立的Option中
     */
    public void parseFeatureData() {
        if (featureData == null || featureData.size() == 0) {
            return;
        }
        for (InsuranceOption option : featureData) {
            if (option != null) {
                if ("Coverage".equals(option.fieldName)) {
                    coverage = option;
                } else if ("Premium".equals(option.fieldName)) {
                    premium = option;
                } else if ("CoveragePeriod".equals(option.fieldName)) {
                    coveragePeriod = option;
                } else if ("PaymentPeriod".equals(option.fieldName)) {
                    paymentPeriod = option;
                } else {
                    Logger.i("->" + option.toString());
                    if (extraData == null) {
                        extraData = new ArrayList<>();
                    }
                    extraData.add(option);
                }
            }
        }
    }

    /**
     * 将独立的Option反解析到featureData中
     */
    public void reparseFeatureData() {
        if (featureData == null) {
            featureData = new ArrayList<>();
        } else {
            featureData.clear();
        }
        featureData.add(coverage == null ? new InsuranceOption() : coverage);
        featureData.add(premium == null ? new InsuranceOption() : premium);
        featureData.add(coveragePeriod == null ? new InsuranceOption() : coveragePeriod);
        featureData.add(paymentPeriod == null ? new InsuranceOption() : paymentPeriod);
        if (extraData != null && extraData.size() > 0) {
            featureData.addAll(extraData);
        }
    }

    public void addInsuredIdBelong(String insuredId) {
        if (TextUtils.isEmpty(insuredId)) {
            return;
        }
        if (insuredIds == null) {
            insuredIds = new ArrayList<>();
        }
        insuredIds.add(insuredId);
    }

    /**
     * 获取豁免人类型： 0 不豁免  1 豁免投保人 2 豁免被保人
     *
     * @return
     */
    public int getExemptionType() {
        if ("Applicant".equals(exemptPerson)) {
            return ProposalConstant.EXEMPTION_TYPE_APPLICANT;
        }
        if ("Insured".equals(exemptPerson)) {
            return ProposalConstant.EXEMPTION_TYPE_INSURED;
        }
        return ProposalConstant.EXEMPTION_TYPE_NO;
    }

    /**
     * 是否是豁免险
     *
     * @return
     */
    public boolean isExemption() {
        return riskType == ProposalConstant.RISK_TYPE_EXEMPT;
    }

}
